// pages/menu/vending-public-commodity-lib/vending-public-commodity-lib.js
var app = getApp()
var verify = require('../../../utils/verify-token.js');
var base64 = require("../../../images/base64");
Page({

  /**
   * 页面的初始数据
   */
  data: { 
	ListData:[],
	page:0,
    totalPage:0,
    loaded:false,
    loading:false,
    nolist:true,
    inputShowed: true,
    filterValue:'',
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
	  icon60: base64.icon60
    })
  
    let that = this;   
  },
  getList:function(){	
	let thm = this;
	let param = {
		filter:'commodityName',
		filterValue:thm.data.filterValue,		
		size:10,
		page:thm.data.page
	};
	wx.request({
	  url: app.globalData.baseUrl+'vending/commodity/public_lib/list',
	  method: 'get',
	  header:{
	    'AUTHORIZATION':app.globalData.token
	  },
	  data:param,
	  success: function (res) {	
      if (res.data.code == 60006) {
        verify.clearAll()
      } else if (res.data.code == 200) {
		  let listdata = res.data.data.content;		 
          let listD = thm.data.ListData;
          let ldh = listD.length;
          for(let i=0;i<listdata.length;i++){
            listD[ldh+i] = listdata[i]
          }
		  
          thm.setData({
            ListData:listD,
            totalPage:res.data.data.totalPages,
            loading:res.data.data.last?false:true,
            loaded:res.data.data.last?true:false
          })
		 // console.log(thm.data.ListData);
      } else {
        wx.showModal({
          title: '操作失败',
          content: res.data.message,
        })
      }
	  }
	})
  },

  // 搜索相关
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  // 取消搜索
  hideInput: function () {
    this.setData({
     	inputShowed: false,
		filterValue:'',
	
    });
	this.search();
    
  },
  // 搜索框输入
  inputTyping: function (e) {
	let that = this;
    that.setData({
      filterValue: e.detail.value
    });
    that.search();
  },

  //搜索
  search: function () {
	let that = this;
    let filterValue = that.data.filterValue;    
    that.setData({
      filterValue: filterValue,
	  ListData:[],
	  page:0,
	  totalPage:0,
    })
    that.getList();
  },
 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	let that = this;
	let filterValue = that.data.filterValue;
	that.setData({
		ListData:[],
		page:0,
		totalPage:0,
		filterValue:filterValue
	});
	that.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
	let that=this;
    let page = that.data.page + 1; 
    if(page<=that.data.totalPage-1){	
    	that.setData({
    		page: page, 
			filterValue:that.data.filterValue
    	})					
    	that.getList();
    }else{
    	that.setData({
    		loaded:true,
    		loading:false,
			filterValue:that.data.filterValue
    	})
    }
	
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})